package com.jade.editor.object.editors;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.wb.swt.SWTResourceManager;
import com.jade.editor.object.editors.parts.DrawArea;
import com.jade.editor.object.editors.parts.StatusBar;
import com.jade.editor.object.editors.parts.ToolBarSide;
import com.jade.editor.object.handler.PathEditorInput;

public class ObjectEditor extends EditorPart {
	public static final String ID = "com.jade.editor.object.editors.ObjectEditor";
	
	private PathEditorInput _input;
	private ObjectModel _objModel;
	private DrawArea _drawArea = null;
	private boolean _dirty = false;
	
	
	public ObjectEditor() {
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		
		_objModel.setBorderColor(_drawArea.getNodeCollector().getBorderColor());
		_objModel.setSelectionColor(_drawArea.getNodeCollector().getSelectionColor());
		_objModel.setConnectionColor(_drawArea.getNodeCollector().getConnectionColor());
		_objModel.setFillColor(_drawArea.getNodeCollector().getFillColor());
		_objModel.setPoleColor(_drawArea.getNodeCollector().getPoleColor());
		_objModel.setPoleSelectionColor(_drawArea.getNodeCollector().getPoleSelectionColor());
		
		_objModel.setDistance(_drawArea.getDrawAreaSize());
		_objModel.setConnected(_drawArea.getNodeCollector().checkNodes());
		
		if(_drawArea.getAnisotropy() != null){
			_objModel.setAniPointsList(_drawArea.getAnisotropy().getPointsIn());
			_objModel.setAniLocation(new Point(0,0));
			_objModel.setAniPath(_drawArea.getAnisotropy().getAniName());
		}
			
		
		if(_drawArea.getNodeCollector().getPole() != null)
			_objModel.hasPole(true);
		else
			_objModel.hasPole(false);
		
		if (_objModel.hasPole())
			_objModel.setPole(_drawArea.getNodeCollector().getPole());
		
		_objModel.setNodeList(_drawArea.getNodeCollector().getNodeList());
		
		if(_drawArea.getBGStatus() == DrawArea.BACKGROUND_NONE){
			_objModel.setBGImageLocation(null);
			_objModel.setBGImagePath(null);
			_objModel.setBGColor(null);
		}
		if(_drawArea.getBGStatus() == DrawArea.BACKGROUND_COLOR){
			_objModel.setBGImageLocation(null);
			_objModel.setBGImagePath(null);
			_objModel.setBGColor(_drawArea.getBGColor());
		}
		if(_drawArea.getBGStatus() == DrawArea.BACKGROUND_IMAGE){
			_objModel.setBGImageLocation(_drawArea.getBGImageLocation());
			_objModel.setBGImagePath(_drawArea.getBGImage().getPath());
			_objModel.setBGColor(_drawArea.getBGColor());
		}
		
		if(_objModel.save(monitor))
			setDirty(false);
	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {

		setSite(site);
		setInput(input);

		if (input instanceof PathEditorInput){
			setSite(site);
			setInput(input);
			setPartName(input.getName());
			_input = (PathEditorInput) input;
			_objModel  = new ObjectModel(_input.getPath().toString());
		}
	}
	
	public void setDirty(boolean b){
		_dirty = b;
		firePropertyChange(IEditorPart.PROP_DIRTY);
	}

	@Override
	public boolean isDirty() {
		return _dirty;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}	
	
	
	@Override
	public void createPartControl(final Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		
		container.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
		container.setLayout(null);
		
		
		_drawArea= new DrawArea(container, _objModel, this);
		new ToolBarSide(container, _drawArea, _input.getPath(), this, _objModel);
		new StatusBar(container, _drawArea);
		

	}
	

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}
}
